<template>
	<view class="detail page bgf5 fs14 c00 pt10" v-if="detail != null">
		<view class="px14 pb12 bgff">
			<view class="flex flexC flexB pt14">
				<text>订单编号：</text>
				<text>{{detail.order_no}}</text>
			</view>
			<view class="flex flexC flexB pt10">
				<text>订单状态</text>
				<text class="c57">{{detail.status_txt}}</text>
			</view>
			<view class="flex flexC flexB pt10">
				<text>店铺名称</text>
				<text>{{detail.shop_name}}</text>
			</view>
			<view class="flex flexC flexB pt10">
				<text>包 厢</text>
				<text>{{detail.room_name}}</text>
			</view>
			<view class="flex flexC flexB pt10">
				<text>订单时间</text>
				<text>{{detail.create_time_text}}</text>
			</view>
			<view class="flex flexC flexB pt10">
				<text>开始时间</text>
				<text>{{detail.book_start}}</text>
			</view>
			<view class="flex flexC flexB pt10">
				<text>结束时间</text>
				<text>{{detail.book_end}}</text>
			</view>

		</view>
		<view class="bgff mt8 px14 pb20">
			<view class="flex flexC flexB borb py12">
				<text>订单金额</text>
				<text class="">￥{{detail.amount}}</text>
			</view>
			<view v-if="detail.tuan != ''" class="flex flexC flexB borb py12">
				<text>团购券使用</text>
				<text class="">{{detail.tuan}}</text>
			</view>


			<view v-if="detail.mt_code != ''" class="flex flexC flexB borb py12">
				<text>美团券使用</text>
				<text class="">{{detail.mt_code}}</text>
			</view>
			<view v-if="detail.dou_code != ''" class="flex flexC flexB borb py12">
				<text>抖音券使用</text>
				<text class="">{{detail.dou_code}}</text>
			</view>
			<view class="flex flexC flexB borb py12">
				<text>余额抵扣</text>
				<text class="">￥{{detail.balance}}</text>
			</view>
			<view class="flex flexC flexB borb py12">
				<text>实付金额</text>
				<text class="cff0">￥{{detail.pay_amount}}</text>
			</view>

			<view class="flex flexC flexB py12">
				<text>开锁密码</text>
				<text class="cff0">{{detail.open_code}}</text>
			</view>

			<view class="flex flexB flexC" v-if="detail.status == 'payed' || detail.status == 'doing'">
				<view @click="open('door')" class="bgf6c r5 aliC py12 mt10 flex1 mr10">开大门</view>
				<view @click="open('room')" class="bgf6c r5 aliC py12 mt10 flex1 mr10 ">开包厢</view>

			</view>

			<view v-if="share != 1">
				<view class="flex flexB flexC" v-if="detail.status == 'payed' || detail.status == 'doing'">
					<button open-type="share" @click="share" class="bgf6c  fs17 aliC py12 mt10 flex1 mr10">分享</button>
					<view v-if="detail.status =='payed' " @click="clickRefund"
						class="bgf6c r5 aliC py12 mt10 flex1 mr10 ">退款</view>
				</view>
			</view>


			<view class="flex flexB flexC" v-if="detail.status == 'payed'">
				<view @click="beforeOpen()" class="bgf6c r5 aliC py12 mt10 flex1 mr10">提前到店</view>
			</view>

			<view class="flex flexB flexC" v-if="detail.status == 'payed' || detail.status == 'doing'">
				<view @click="xuOrder" class="bgf6c r5 aliC py12 mt10 flex1 mr10">续费</view>

			</view>
		</view>

		<!-- 续费弹框 -->
		<uni-popup type="center" ref="popupXufei">
			<xufei v-if="yu != null" :item="yu" @xufeiPay="xufeiPay" @closeYuding="closeYuding"></xufei>
		</uni-popup>


	</view>
</template>

<script>
	const plugin = requirePlugin("myPlugin");
	
	export default {
		data() {
			return {
				order_no: '',
				detail: null,
				share: -1,
				yu: null

			}
		},
		onLoad(option) {

			this.order_no = option.order_no;
			if (option.share) {
				this.share = 1;
			}
			this.getDetail();
		},

		onShareAppMessage(res) {
			console.log(res);
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: this.detail.room_name + ",快来玩呀", //自定义分享标题
				path: '/pages/order/detail?order_no=' + this.detail.order_no +
				"&share=1", // '/pages/...'分享页面路径（打开分享时进入页），以/开头
			}
		},
		methods: {
			closeYuding() {
				this.$refs.popupXufei.close();
			},
			xuOrder() {
				let param = {
					order_no: this.order_no
				};
				let that = this;
				this.$api.xu(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.yu = res.data;
						this.$refs.popupXufei.open();
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}

				})
			},


			xufeiPay(item) {
				let param = {
					id: item.id,
					hour: item.hour
				};
				let that = this;
				this.$api.xufeiPay(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.closeYuding();
						that.order_no = res.data.order_no;
						if (res.data.payInfo != '') {
							that.wechatPay(res.data.payInfo)
						} else {
							uni.redirectTo({
								url: '../order/detail?order_no=' + that.order_no
							})
						}

					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}

				})
			},

			wechatPay(payConfig) {
				let that = this;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: payConfig.payTimeStamp,
					nonceStr: payConfig.paynonceStr,
					package: payConfig.payPackage,
					signType: payConfig.paySignType,
					paySign: payConfig.paySign,
					success: function(res) {

						uni.redirectTo({
							url: '../order/detail?order_no=' + that.order_no
						})

					},
					fail: function(err) {
						console.info(err);
						that.restore();
						uni.showToast({
							title: "取消续费",
							icon: 'none'
						});
					}
				});
			},



			beforeOpen() {
				let param = {
					order_no: this.order_no
				};
				let that = this;
				this.$api.beforeOpen(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {


					}
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				})
			},

			clickRefund() {
				let that = this;
				uni.showModal({
					title: "退款提示",
					content: "您是否确认退款",
					confirmText: "退款",
					complete(res) {
						if (res.confirm) {
							that.refund();
						}
					}
				})
			},

			refund() {

				let param = {
					order_no: this.order_no,
				};
				let that = this;
				this.$api.orderRefund(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.getDetail();
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},

			open(item) {


				let param = {
					type: item,
					order_no: this.order_no
				};
				let that = this;
				this.$api.open(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {

						if (res.data != null && res.data.is_bt == 1) {
							that.getBtLockData(res.data);

						}
					}
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				})
			},

			getBtLockData(data) {


				let param = {
					shop_id: data.shop_id,
					room_id: data.room_id,
				}
				let that = this;
				this.$api.btLockInfo(param).then(res => {
					if (res.code == 1) {
						console.info(res.data);
						that.openLock(res.data);
					} else {

					}

				})


			},

			openLock(res) {


				uni.showLoading({
					title: "正在开启智能锁",
				})
				const start = Date.now();
				// 调用开锁接口


				var deviceId = res.lockMac;
				var lockData = res.lockData;
				plugin.controlLock(plugin.ControlAction.OPEN, lockData, res => {
					if (res.errorCode === 10003) {
						console.log("获取版本信息时设备连接已断开", res)
					}
				}, null, deviceId).then(res => {
					uni.hideLoading({});
					console.log(res)
					if (res.errorCode == 10012) {
						uni.showModal({
							title: "提示",
							content: "蓝牙未开启,请开启蓝牙",
							showCancel: false,

						})
					}
					if (!!res.deviceId) deviceId = res.deviceId;
					if (res.errorCode === 0) {
						console.info(`已开锁--操作时间:${Date.now() - start}ms.`)
					} else {

						console.info(`开锁失败: ${res.errorMsg}`)
					}
				})
			},




			getDetail() {

				let param = {
					order_no: this.order_no,
				};
				let that = this;
				this.$api.orderDetail(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.detail = res.data;
					} else {

					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	button {
		padding: 0;
	}
</style>